<template>
  <div>
  <el-form style="width: 60%" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="书籍编号" prop="bookId">
      <el-input v-model="ruleForm.bookId"></el-input>
    </el-form-item>
    <el-form-item label="书籍名称" prop="bookName">
      <el-input v-model="ruleForm.bookName"></el-input>
    </el-form-item>
    <el-form-item label="书籍作者" prop="bookAuthor">
      <el-input v-model="ruleForm.bookAuthor"></el-input>
    </el-form-item>
    <el-form-item label="书籍价格" prop="price">
      <el-input v-model="ruleForm.price"></el-input>
    </el-form-item>
    <el-form-item label="出版社" prop="address">
      <el-input v-model="ruleForm.address"></el-input>
    </el-form-item>
    <el-form-item label="印刷版本" prop="impression">
      <el-input v-model="ruleForm.impression"></el-input>
    </el-form-item>
    <el-form-item label="书籍描述" prop="introduce">
      <el-input v-model="ruleForm.introduce"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="update('ruleForm')">修改</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        ruleForm: {
          bookId:'',
          bookName: '',
          bookAuthor: '',
          price: '',
          address: '',
          impression: '',
          introduce: ''
        },
        rules: {
          bookId:[
            { required: true, message: '请输入书籍编号', trigger: 'blur' }
          ],
          bookName: [
            { required: true, message: '请输入书籍名称', trigger: 'blur' }
          ],
          bookAuthor:[
            { required: true, message: '请输入作者名称', trigger: 'blur' }
          ],
          price:[
            { required: true, message: '请输入书籍价格', trigger: 'blur' }
          ],
          address:[
            { required: true, message: '请输入书籍出版社', trigger: 'blur' }
          ],
          impression:[
            { required: true, message: '请输入书籍版本', trigger: 'blur' }
          ],
          introduce:[
            { required: true, message: '请输入书籍描述', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {

      update(formName) {
        const _this = this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            axios.put('http://localhost:8181/book/update',this.ruleForm).then(function (resp) {
              if(resp.data=='success'){
                // alert("添加成功") 跳转的路由
                _this.$alert('《'+_this.ruleForm.bookName+'》修改成功', '消息', {
                  confirmButtonText: '确定',
                  callback: action => {
                    _this.$router.push('/page1')
                  }
                });

              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    created() {
      const _this = this
      axios.get('http://localhost:8181/book/findOneBook/'+this.$route.query.id).then(function (resp) {
        _this.ruleForm=resp.data.books
      })

    }
  }
</script>
